package com.example.backend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.backend.dto.StatsDTO;
import com.example.backend.entity.Book;
import com.example.backend.entity.Borrow;
import com.example.backend.entity.User;
import com.example.backend.mapper.BookMapper;
import com.example.backend.mapper.BorrowMapper;
import com.example.backend.mapper.UserMapper;
import com.example.backend.service.StatsService;
import org.springframework.stereotype.Service;

@Service
public class StatsServiceImpl implements StatsService {
    
    private final BookMapper bookMapper;
    private final UserMapper userMapper;
    private final BorrowMapper borrowMapper;
    
    public StatsServiceImpl(BookMapper bookMapper, UserMapper userMapper, BorrowMapper borrowMapper) {
        this.bookMapper = bookMapper;
        this.userMapper = userMapper;
        this.borrowMapper = borrowMapper;
    }
    
    @Override
    public StatsDTO getStats() {
        StatsDTO stats = new StatsDTO();
        
        // 图书总数
        stats.setBookCount(bookMapper.selectCount(null));
        
        // 用户总数（不包括管理员）
        LambdaQueryWrapper<User> userWrapper = new LambdaQueryWrapper<>();
        userWrapper.eq(User::getType, 1);
        stats.setUserCount(userMapper.selectCount(userWrapper));
        
        // 借阅总数（已借出）
        LambdaQueryWrapper<Borrow> borrowWrapper = new LambdaQueryWrapper<>();
        borrowWrapper.eq(Borrow::getStatus, 1);
        stats.setBorrowCount(borrowMapper.selectCount(borrowWrapper));
        
        // 待审核数
        LambdaQueryWrapper<Borrow> pendingWrapper = new LambdaQueryWrapper<>();
        pendingWrapper.eq(Borrow::getStatus, 0);
        stats.setPendingCount(borrowMapper.selectCount(pendingWrapper));
        
        return stats;
    }
} 